package com.shop.main;

import java.io.File;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.shop.cache.api.client.io.SCClientFactory;
import com.shop.cache.api.client.io.SCClientManager;
import com.shop.cache.api.client.main.SCCache;
import com.shop.cache.api.client.main.SCDataBlock;
import com.shop.cache.api.storage.SCStorage;
import com.shop.cache.imp.common.ShopComCacheFactory;
import com.shop.cache.imp.storage.ccdb2.CCDB2Parameters;
import com.shop.cache.imp.storage.ccdb2.CCDB2StorageFactory;

public class ShopLocalCacheClient {
	
	private static File getFileFromPath(String path)
	{
		File 	f = new File(path);
		if ( !f.exists() && !f.mkdirs() )
		{
			throw new IllegalArgumentException("Could not make directory: " + f.getPath());
		}
		return f;
	}
	
	public static void testPutAndGet(){
		try{
			Set hobby = new HashSet();
			hobby.add("TV");
			hobby.add("game");
			hobby.add("girl");
			
			Human qjb = new Human();
			qjb.setName("屈江波");
			qjb.setGender(1);
			qjb.setHobby(hobby);
			
			hobby = new HashSet();
			hobby.add("football");
			hobby.add("basketball");
			hobby.add("dota");
			
			Human qyf = new Human();
			qyf.setName("乔彦飞");
			qyf.setGender(1);
			qyf.setHobby(hobby);
			
			SCStorage db = CCDB2StorageFactory.create(new CCDB2Parameters());
			db.open(getFileFromPath("localcache"));
			
			SCClientFactory clientFactory = ShopComCacheFactory.getClientFactory();
			SCClientManager manager = clientFactory.newLocalClientManager(db);
			SCCache myCache = new SCCache(manager);
			
			SCDataBlock block = new SCDataBlock(qjb.getName(), qjb);
			block.canBeStoredInMemory(false);
			block.canBeQueued(false);
			
			long startTime = System.currentTimeMillis();
			
			myCache.put(block);
			
			System.out.println((System.currentTimeMillis() - startTime) + "----qjb of ttl: " + block.getTTL());
			
			block = new SCDataBlock(qyf.getName(), qyf);
			block.canBeStoredInMemory(false);
			block.canBeQueued(false);
			
			System.out.println("qyf of ttl: " + block.getTTL());
			
			myCache.put(block);
			
			System.out.println("================================================");
			
			Human tmp = (Human)myCache.get(new SCDataBlock(qjb.getName(), null));
			
			System.out.println(tmp);
			
			long ttl = myCache.getTTL(qjb.getName());
			
			System.out.println(ttl);
			
			System.out.println("================================================");
			
			tmp = (Human)myCache.get(new SCDataBlock(qyf.getName(), null));
			
			System.out.println(tmp);
			
			ttl = myCache.getTTL(qyf.getName());
			
			System.out.println(ttl);
			
			System.out.println("================================================");
			
			tmp = (Human)myCache.get(new SCDataBlock("chenbutao", null));
			
			System.out.println(tmp);
			
			ttl = myCache.getTTL("chenbutao");
			
			System.out.println(ttl);
			
			myCache.close();
			System.out.println("release.");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void testAfterGet(){
		try{
			String key = "屈江波";
			
			SCStorage db = CCDB2StorageFactory.create(new CCDB2Parameters());
			db.open(getFileFromPath("localcache"));
			
			SCClientFactory clientFactory = ShopComCacheFactory.getClientFactory();
			SCClientManager manager = clientFactory.newLocalClientManager(db);
			SCCache myCache = new SCCache(manager);
			
			SCDataBlock block = new SCDataBlock(key, null);
			block.canBeStoredInMemory(false);
			block.canBeQueued(false);
			block.ignoreTTL(true);
			
			long startTime = System.currentTimeMillis();
			
			Human tmp = (Human)myCache.get(block);
			
			System.out.println((System.currentTimeMillis() - startTime) + "----" + tmp);
			
			myCache.remove("屈江波");
			
			myCache.close();
			System.out.println("release.");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws Exception{
//		testPutAndGet();
		testAfterGet();
	}
	
}

class Human implements Serializable {
	
	private String name;
	
	private int gender;
	
	private Set hobby;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getGender() {
		return gender;
	}

	public void setGender(int gender) {
		this.gender = gender;
	}

	public Set getHobby() {
		return hobby;
	}

	public void setHobby(Set hobby) {
		this.hobby = hobby;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stubor 
		String hobbyStr = "";
		
		for(Iterator it = hobby.iterator(); it.hasNext(); ){
			hobbyStr += "," + it.next().toString();
		}
		
		hobbyStr = hobbyStr.substring(1);
		
		return name + "[性别：" + (gender == 0 ? "女" : "男") + ", 爱好{" + hobbyStr + "}].";
	}
	
}